package interviewClassic150;

import org.junit.Test;

import java.util.HashSet;
import java.util.Set;

public class LengthOfLongestSubstring {
    public int lengthOfLongestSubstring(String s) {
        if (s.length() == 0) return 0;
        int left = 0;//滑动窗口左边界

        //定义一个set来存储当前滑动窗口中的字母
        Set<Character> set = new HashSet<>();
        set.add(s.charAt(0));
        int res = 1;
        for (int i = 1; i < s.length(); i++) {
            while (left < i && set.contains(s.charAt(i))) {
                set.remove(s.charAt(left));
                left++;
            }
            set.add(s.charAt(i));
            int curLen = i - left + 1;
            res = Math.max(res,curLen);
        }

        return res;

    }

    @Test
    public void test() {
        String s = "pwwkew";
        int i = lengthOfLongestSubstring(s);
        System.out.println(i);
    }
}
